﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using QunYing.BLL;

namespace QunYing.Web
{
    public partial class ForgetPwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HideTxt();
            }
        }

        private void HideTxt()
        {
            lblAlert.Visible = false;
            lblNew.Visible = false;
            lblPwd.Visible = false;
        }
        private void ShowTxt()
        {
            lblAlert.Visible = true;
            lblNew.Visible = true;
            lblPwd.Visible = true;
        }

        protected void btnGo_Click(object sender, ImageClickEventArgs e)
        {
            HideTxt();
            string strId = TextBox1.Text.Trim();
            string strTel = TextBox2.Text.Trim();
            QunYing.BLL.UserLoginInfo info = new UserLoginInfo();
            DataSet ds = info.GetList("用户ID='" + strId + "' AND 手机号码='" + strTel + "' AND 角色='会员'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                string newPwd = MakePassword(6);
                QunYing.Model.UserLoginInfo model = info.GetModel(strId);
                model.密码 = newPwd;
                bool b = info.Update(model);
                if (b)
                {
                    lblPwd.Text = newPwd;
                    ShowTxt();
                }
                else
                {
                    MessageBox.Show(this.UpdatePanel1,"找回密码失败，请重试!");
                }
            }
            else
            {
                MessageBox.Show(this.UpdatePanel1, "用户名与手机号码不匹配,找回密码失败!");
            }

        }

        private string MakePassword(int pwdlen)
        {
            string pwdchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string tmpstr = "";
            int iRandNum;
            Random rnd = new Random();
            for (int i = 0; i < pwdchars.Length; i++)
            {
                iRandNum = rnd.Next(pwdchars.Length);
                tmpstr += pwdchars[iRandNum];
            }
            if (tmpstr.Length > 6)
            {
                tmpstr = tmpstr.Substring(0, 6);
            }
            return tmpstr;
        }
    }
}
